package com.shinhye.smartsms;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.app.ListActivity;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.shinhye.smartsms.utils.CommonUtils;
import com.shinhye.smartsms.utils.MyAsyncQueryHandler;
import com.shinhye.smartsms.utils.SmsUris;
import com.shinhye.smartsms.utils.MyAsyncQueryHandler.OnChangeCursorListener;


public class FolderActivity extends ListActivity implements OnChangeCursorListener{
	private int[] imgids = {
			R.drawable.a_f_inbox, R.drawable.a_f_outbox, R.drawable.a_f_sent,R.drawable.a_f_draft
	};
	private String[] names = {
			"收件箱", "发件箱", "已发送", "草稿箱"
	};
	private ListView lv_folder_main;
	private SimpleAdapter adapter;
	private ArrayList<Map<String,Object>> list;
	private MyAsyncQueryHandler asyncQueryHandler;
	private ContentObserver observer;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		lv_folder_main = getListView();
		
		asyncQueryHandler = new MyAsyncQueryHandler(getContentResolver());
		asyncQueryHandler.setOnChangeCursorListener(this);
		getData();
		
		list = new ArrayList<Map<String,Object>>();
		for (int i = 0; i < imgids.length; i++) {
			HashMap<String,Object> map = new HashMap<String,Object>();
			map.put("image", imgids[i]);
			map.put("name", names[i]);
			map.put("number", 0);
			list.add(map);
		}
		
		adapter = new SimpleAdapter(this, list, R.layout.item_folder_main, new String[]{"image", "name", "number"}, new int[]{R.id.iv_icon, R.id.tv_name, R.id.tv_count});
		lv_folder_main.setAdapter(adapter);
		lv_folder_main.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(FolderActivity.this, FolderDetailActivity.class);
				intent.putExtra("type", position);
				startActivity(intent);
			}
		});
		
		registObserver();
	}

	private void getData() {
		for(int i = 0; i < imgids.length; i++) {
			asyncQueryHandler.startQuery(i, null, CommonUtils.getUri(i), new String[]{"count(*)"}, null, null, null);
		}
	}
	
	private void registObserver() {
		observer = new ContentObserver(new Handler()) {
			@Override
			public void onChange(boolean selfChange) {
				getData();
			}
		};
		getContentResolver().registerContentObserver(SmsUris.SMS_URI, true, observer);
	}

	@Override
	public void preFunc(int token, Object cookie, Cursor cursor) {}

	@Override
	public void afterFunc(int token, Object cookie, Cursor cursor) {
		cursor.moveToFirst();
		int count = cursor.getInt(0);
		list.get(token).put("number", count);
		adapter.notifyDataSetChanged();
	}
	
	@Override
	protected void onDestroy() {
		getContentResolver().unregisterContentObserver(observer);
		observer = null;
		super.onDestroy();
	}
}
